#include "thread/executor.h"
#include "base/callback.h"
#include "base/logging.h"
#include "gtest/gtest.h"


int global_var = 1;
void TestCallback(int i) {
  global_var = i;
}

TEST(ExecutorTest, TestCallbackThread) {
  global_var = 1;
  CallbackThread t(NewCallback(&TestCallback, 6));
  t.Start();
  t.Join();
  EXPECT_EQ(6, global_var);
}

TEST(ExecutorTest, TestCallbackThreadWithDoneCallback) {
  global_var = 1;
  CallbackThread t(NewCallback(&TestCallback, 6));
  t.set_done_callback(NewCallback(&TestCallback, 12));
  t.Start();
  t.Join();
  EXPECT_EQ(12, global_var);
}


void Sleep(int num_sec) {
  LOG(INFO) << "prepare to sleep";
  sleep(num_sec);
  LOG(INFO) << "end sleep";
}

TEST(ExecutorTest, TestExecutor) {
  DefaultExecutor executor(2);
  executor.Add(NewCallback(&Sleep, 3));
  executor.Add(NewCallback(&Sleep, 7));
  executor.Add(NewCallback(&Sleep, 5));
}

TEST(ExecutorTest, TestExecutorAfter) {
  DefaultExecutor executor(2);
  executor.AddAfter(5000000, NewCallback(&Sleep, 3));
  executor.AddAfter(5000000, NewCallback(&Sleep, 5));
  executor.AddAfter(5000000, NewCallback(&Sleep, 6));
}
